package controller

// 将 model.DB 改为 config.DB

import (
	"net/http"
	
	"github.com/gin-gonic/gin"
	"software/model"
	"software/config"
	"gorm.io/gorm"
)

// 获取当前用户所有面试场景
func GetInterviewScenes(c *gin.Context) {
	userID := c.GetUint("user_id")
	var scenes []model.InterviewScene
	if err := config.DB.Where("user_id = ?", userID).Find(&scenes).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "获取面试场景失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"success": true, "data": scenes})
}

// 获取单个面试场景详情
func GetInterviewScene(c *gin.Context) {
	id := c.Param("id")
	userID := c.GetUint("user_id")
	var scene model.InterviewScene
	if err := config.DB.Where("id = ? AND user_id = ?", id, userID).First(&scene).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			c.JSON(http.StatusNotFound, gin.H{"success": false, "message": "面试场景不存在"})
		} else {
			c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "获取面试场景失败"})
		}
		return
	}
	c.JSON(http.StatusOK, gin.H{"success": true, "data": scene})
}

// 新增面试场景
func CreateInterviewScene(c *gin.Context) {
	userID := c.GetUint("user_id")
	var req model.InterviewScene
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "参数错误"})
		return
	}
	req.UserID = userID
	if err := config.DB.Create(&req).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "创建失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"success": true, "data": req})
}

// 编辑面试场景
func UpdateInterviewScene(c *gin.Context) {
	id := c.Param("id")
	userID := c.GetUint("user_id")
	var req model.InterviewScene
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "参数错误"})
		return
	}
	var scene model.InterviewScene
	if err := config.DB.Where("id = ? AND user_id = ?", id, userID).First(&scene).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"success": false, "message": "面试场景不存在"})
		return
	}
	// 只允许部分字段更新
	scene.Name = req.Name
	scene.InterviewerGender = req.InterviewerGender
	scene.InterviewerStyle = req.InterviewerStyle
	scene.QuestionCountMin = req.QuestionCountMin
	scene.QuestionCountMax = req.QuestionCountMax
	scene.Difficulty = req.Difficulty
	scene.ResumeID = req.ResumeID
	scene.Domains = req.Domains
	if err := config.DB.Save(&scene).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "更新失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"success": true, "data": scene})
}

// 删除面试场景
func DeleteInterviewScene(c *gin.Context) {
	id := c.Param("id")
	userID := c.GetUint("user_id")
	var scene model.InterviewScene
	if err := config.DB.Where("id = ? AND user_id = ?", id, userID).First(&scene).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"success": false, "message": "面试场景不存在"})
		return
	}
	if err := config.DB.Delete(&scene).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "删除失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"success": true})
} 